package com.zjuh.qss.web.action.admin;

import java.io.Writer;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import wint.help.sql.SqlUtil;
import wint.lang.utils.IoUtil;
import wint.mvc.flow.FlowData;
import wint.mvc.template.Context;

import com.zjuh.qss.web.common.BaseAction;

public class Misc extends BaseAction {
	
	private static final Logger log = LoggerFactory.getLogger(Misc.class);
	
	private DataSource dataSource;
	
	public void exportNicks(FlowData flowData, Context context) {
		flowData.setViewType("nop");
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		Writer out = null;
		try {
			conn = dataSource.getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery("select distinct seller_nick from qss_topic_item");
			
			out = flowData.getWriter();
			int i = 0;
			while (rs.next()) {
				String sellerNick = rs.getString("seller_nick");
				out.write(sellerNick);
				out.write("\r\n");
				++i;
				if (i % 100 == 0) {
					out.flush();
				}
			}
			out.flush();
		} catch (Exception e) {
			log.error("exportNicks error", e);
		} finally {
			SqlUtil.close(rs);
			SqlUtil.close(stmt);
			SqlUtil.close(conn);
			
			IoUtil.close(out);
		}
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

}
